import axios from '@/libs/api.request'
import qs from 'qs'

/**
 * @Description: 获取所有学院
 * @Params: {}
 * @Return: { Object } 学院列表
 * @Date: 2019/1/25 20:15
 */
export const getInstituteList = () => {
  return axios.request({
    url: '/labm/getInstitute',
    method: 'GET'
  })
}

/**
 * @Description: 获取用户列表
 * @Params: { Object } filter 检索条件，pageBuilder 分页条件
 * @Return: { Object } 用户列表
 * @Date: 2019/1/29 1:53
 */
export const getUserList = (filter, pageBuilder) => {
  let userDTO = {...filter}
  userDTO.identity = userDTO.identity === 'all' ? '' : userDTO.identity
  userDTO.enabled = userDTO.enabled === 'all' ? '' : !!userDTO.enabled
  userDTO.roles = userDTO.roles === 'all' ? [] : [userDTO.roles]
  userDTO.departments = userDTO.departments === 'all' ? [] : [userDTO.departments]
  userDTO.pageBuilder = {...pageBuilder}
  userDTO.pageBuilder.order = pageBuilder.order.filter(item => {
    return item[Object.keys(item)[0]] !== 'normal'
  })
  return axios.request({
    url: '/uaa/user/getAll',
    method: 'POST',
    data: JSON.stringify(userDTO)
  })
}

/**
 * @Description: 修改用户状态
 * @Params: { Array, String } ids 需要修改状态的用户id，status 状态
 * @Return: { Object } 修改用户状态结果
 * @Date: 2019/2/18 13:41
 */
export const updateUserStatus = (ids, status) => {
  return axios.request({
    url: '/uaa/user/updateStatus',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify({
      ids: ids,
      isEnable: status
    }, { arrayFormat: 'brackets' })
  })
}

/**
 * @author Dinosaur
 * @microservice information
 * @description 获得学院所有教师
 * @param { String } institue 学院
 * @returns 教师列表
 */
export const getTeacherList = (institute) => {
  return axios.request({
    url: '/info/tea/getTeachersByInstitute',
    method: 'GET',
    params: {
      institute: institute || ''
    }
  })
}

/**
 * @author Dinosaur
 * @microservice oauth-server
 * @description 提交用户 新增/修改
 * @param {String} data
 * @returns 是否 添加/修改 成功
 */
export const submitSaveUser = (data) => {
  return axios.request({
    url: '/uaa/user/save',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: data
  })
}

/**
 * @author Dinosaur
 * @microservice oauth-server
 * @description 修改用户权限
 * @param { Number } id
 * @param { Array } roles
 * @returns 是否修改成功
 */
export const updateUserRoles = (id, roles) => {
  return axios.request({
    url: '/uaa/user/updateUserRoles',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
      // 'Content-Type': 'application/json'
    },
    data: qs.stringify({
      id: id,
      roles: roles
    }, { arrayFormat: 'brackets' })
  })
}

/**
 * @author Dinosaur
 * @microservice oauth-server
 * @description 根据工号获取用户信息
 * @param { String } code 工号
 * @returns { Object } 用户信息
 */
export const getUserByCode = (code) => {
  return axios.request({
    url: '/uaa/user/getUserInfo',
    method: 'GET',
    params: {
      code: code
    }
  })
}

/**
 * @author Dinosaur
 * @microservice oauth-server
 * @description 根据ID获取用户信息
 * @param { Number } id ID
 * @returns { Object } 用户信息
 */
export const getUserById = (id) => {
  return axios.request({
    url: '/uaa/user/getUserInfo',
    method: 'GET',
    params: {
      id: id
    }
  })
}

/**
 * @author Dinosaur
 * @microservice oauth-server
 * @description 获得权限类别
 * @returns 权限类别
 */
export const getRolesType = () => {
  return axios.request({
    url: '/uaa/getRolesType',
    method: 'GET'
  })
}
